﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public struct CellVo
{
    public int gx;
    public int gy;
    /// <summary>
    /// 当前格子索引
    /// </summary>
    public int index;
    /// <summary>
    /// 当前格子类型
    /// </summary>
    public int type;

    public void SetData(int gx, int gy, int index,int type)
    {
        this.gx = gx;
        this.gy = gy;
        this.index = index;
        this.type = type;
    }
}

public class ItemCell : MonoBehaviour,IPointerDownHandler,IPointerUpHandler,IPointerMoveHandler,IPointerEnterHandler,IPointerExitHandler
{
    public CellVo data;

    public Vector3 pos;

    private RectTransform rectTransform_parent;
    private RectTransform rectTransform;

    private Image icon;
    private Image mask;
    private Color[] colors = new Color[5] { Color.cyan, Color.blue, Color.white, Color.green, Color .red};

    private void Awake()
    {
        this.rectTransform = this.transform.GetComponent<RectTransform>();
        this.rectTransform_parent = this.transform.parent.GetComponent<RectTransform>();

        Vector2 screenPos = RectTransformUtility.WorldToScreenPoint(UIMrg.Instance.UICamera, this.rectTransform.anchoredPosition3D); // 将世界坐标转换为屏幕坐标

        Vector2 localPos;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(this.rectTransform_parent, screenPos, null, out localPos))
        {
            Debug.Log("UI坐标：" + localPos);

            // 这里可以根据localPos来操作UI元素或者其他逻辑处理
        }
        else
        {
            Debug.LogError("无法从屏幕坐标到本地坐标");
        }

        this.icon = GameObjectTools.Find<Image>("Icon", this.gameObject);
        this.mask = GameObjectTools.Find<Image>("Mask", this.gameObject);
    }
    private void Start()
    {
        
    }
    //是否显示遮罩
    public void ShowMask(bool isShow)
    {
        this.mask.gameObject.SetActive(isShow);
    }
    public void SetData(CellVo value)
    {
        this.ShowMask(false);
        this.data = value;
        this.icon.color = this.colors[this.data.type];
         
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        ItemCell itemCell= eventData?.pointerEnter?.transform?.parent.GetComponent<ItemCell>();
        if(itemCell!=null) LogicMrg.Instance.SetType(itemCell.data.type);
    }
    void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
    {
    }


    void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
    {
        if (!LogicMrg.Instance.isAllow) return;
        ItemCell itemCell = eventData.pointerEnter.transform?.parent.GetComponent<ItemCell>();
        if (itemCell)
        {
            Debug.Log(itemCell.name);
            DrowLineMrg.Instance.Add(itemCell);
        }
    }

    void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
    {
    }

    void IPointerMoveHandler.OnPointerMove(PointerEventData eventData)
    {
    }

    
}
